﻿using System.Globalization;
using System.Runtime.InteropServices;

namespace Microsoft.Xna.Framework.Input
{
    /// <summary>
    /// Structure that represents the position of left and right sticks (thumbsticks) on an Xbox 360 Controller.
    /// </summary>
	[StructLayout(LayoutKind.Sequential)]
    public struct GamePadThumbSticks
    {
        #region Fields
        internal Vector2 _left;
        internal Vector2 _right;
        #endregion

        #region Properties
        /// <summary>
        /// Returns the position of the left Xbox 360 Controller stick (thumbstick) as a 2D vector.
        /// </summary>
        public Vector2 Left
        {
            get { return _left; }
        }

        /// <summary>
        /// Returns the position of the right Xbox 360 Controller stick (thumbstick) as a 2D vector.
        /// </summary>
        public Vector2 Right
        {
            get { return _right; }
        }
        #endregion 

        #region Constructor
        /// <summary>
        /// Initializes a new instance of the GamePadThumbSticks class.
        /// </summary>
		/// <param name="leftThumbstick">Left stick value. Each axis is clamped between -1.0 and 1.0.</param>
		/// <param name="rightThumbstick">Right stick value. Each axis is clamped between -1.0 and 1.0.</param>
		public GamePadThumbSticks(Vector2 leftThumbstick, Vector2 rightThumbstick)
		{
			_left = Vector2.Clamp(leftThumbstick, new Vector2(-1f, -1f), Vector2.One);
			_right = Vector2.Clamp(rightThumbstick, new Vector2(-1f, -1f), Vector2.One);
		}
        #endregion

        #region Public Methods
        /// <summary>
        /// Returns a value that indicates whether the current instance is equal to a specified object.
        /// </summary>
        /// <param name="obj">Object with which to make the comparison.</param>
        public override bool Equals(object obj)
        {
            return (obj is GamePadThumbSticks) ? ((GamePadThumbSticks)obj) == this : false;
        }

        /// <summary>
        /// Gets the hash code for this instance.
        /// </summary>
        public override int GetHashCode()
        {
            return _left.GetHashCode() ^ _right.GetHashCode();
        }

        /// <summary>
        /// Retrieves a string representation of this object.
        /// </summary>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture, "{{Left:{0} Right:{1}}}", _left, _right);
        } 
        #endregion

        #region Operators
        /// <summary>
        /// Determines whether two GamePadThumbSticks instances are not equal.
        /// </summary>
        /// <param name="left">Object on the left of the equal sign.</param>
        /// <param name="right">Object on the right of the equal sign.</param>
        public static bool operator !=(GamePadThumbSticks left, GamePadThumbSticks right)
        {
            return ((left._left != right._left) || (left._right != right._right));
        }

        /// <summary>
        /// Determines whether two GamePadThumbSticks instances are equal.
        /// </summary>
        /// <param name="left">Object on the left of the equal sign.</param>
        /// <param name="right">Object on the right of the equal sign.</param>
        public static bool operator ==(GamePadThumbSticks left, GamePadThumbSticks right)
        {
            return ((left._left == right._left) && (left._right == right._right));
        } 
        #endregion
    }
}
